// 1- 可以使用第三方模块 http-proxy-middleware
//    cnpm install http-proxy-middleware
const express = require("express");
const {createProxyMiddleware} = require("http-proxy-middleware");//
const app = express();
app.use(express.static(__dirname));
// 不限制请求方式，请求必须以/api目录开头
// http://127.0.0.1:8000/api
// http://127.0.0.1:8000/api/a/b
// 配置代理服务器：必须以/api开头才可以使用该代理服务。
// ajax-->/api/info
// http://127.0.0.1:9000/info
app.use("/api",createProxyMiddleware({
	// 代理帮助请求的目标服务器
	target:"http://127.0.0.1:9000",
	changeOrigin:true,// 默认为false
	pathRewrite:{
		"^/api":"",// 将/api开头的部分用空字符串替换掉，然后再与 target进行拼接。
	}
}));
//          https://i.maoyan.com/api/mmdb/movie/v3/list/hot.json?ct=北京&ci=1&channelId=4
// ajax---> https://i.maoyan.com/api/mmdb/movie/v3/list/hot.json?ct=北京&ci=1&channelId=4
app.use("/maoyan",createProxyMiddleware({
	target:"https://i.maoyan.com/api/mmdb/movie/v3/list",
	changeOrigin:true,
	pathRewrite:{
		"^/maoyan":""
	}
}))
app.get("/info",(req,res)=>{
	res.json({
		ok:1,
		msg:"8000get->info"
	})
});
app.listen(8000,()=>{
	console.log("success");
})